var util = {
	isShow: function($el) {
		var scroll_top = $(window).scrollTop(),
			window_height = $(window).height(),
			offset_top = $el.offset().top,
			height = $el.height();
		return $el.is(':visible') && scroll_top <= offset_top + height && scroll_top >= offset_top - window_height;
	},
	/**
	 * 背景图的懒加载
	 * @param {String} options.selector 选择器的名称，必须为类选择器
	 * @param {String} options.error 图片加载失败显示内容，默认<i class="icon icon-lazy icon-lazy-error">暂无封面图</i>
	 */
	lazyBackgroundImg: function(options) {
		var that = this,
			$lazys = $('.' + options.selector),
			delayTime = options.delayTime || 300,
			error = options.error || '<i class="icon icon-lazy icon-lazy-error">暂无封面图</i>',
			loadBgImg = function() {
				clearTimeout(y);
				if ($lazys.length) {
					var showLazys = [];
					for (var i = 0; i < $lazys.length; i++) {
						that.isShow($($lazys[i])) && showLazys.push(i);
					}
					if (showLazys.length) {
						var l = showLazys.length - 1,
							r = that.randomNum(0, l),
							i = showLazys[r],
							$el = $($lazys[i]),
							url = $el.attr('data-url'),
							$img = $('<img src="' + url + '"/>');
						$el.css('background', "url('" + url + "')");
						$img.error(function() {
							$el.append(error);
							$el.css('background', '');
						});
						$el.removeClass(options.selector);
						$lazys.splice(i, 1);
						var y = setTimeout(function() {
							loadBgImg();
						}, delayTime);
					}
				} else {
					$(window).off('scroll', delayScroll);
				}
			}
		if ($lazys.length) {
			loadBgImg();
			$(window).on('scroll', delayScroll);

			function delayScroll() {
				clearTimeout(d);
				console.log('delayTime',delayTime)
				var d = setTimeout(function() {
					loadBgImg();
				}, delayTime);
			}
		}
	},
	randomNum: function(minNum, maxNum) {
		switch (arguments.length) {
			case 1:
				return parseInt(Math.random() * minNum + 1, 10);
			case 2:
				return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
			default:
				return 0;
		}
	}
}
